package web_package

import org.hibernate.cfg.Configuration
import db.User
import db.UserManager
import db.Folder

object SignIn {
	def signIn(usr : User) : User = {
		val cfg = new Configuration()
		var sessionFactory = null.asInstanceOf[org.hibernate.SessionFactory]
		var session_ = null.asInstanceOf[org.hibernate.Session]
	    try{
	    	sessionFactory = cfg.configure().buildSessionFactory()
	    	session_ = sessionFactory.openSession()
	    	val manager = new UserManager(session_)
	    	manager.saveUser(usr)
	    	var query = session_.createQuery("from Folder.folderID")
	    	val list = query.list.toArray.asInstanceOf[Array[Long]]
	    	var max = list.max
	    	val folder = new Folder
	    	folder.setFolderID(max+1)
	    	folder.setFolderName("Clever Dropbox")
	    	folder.setUserID(usr.getUserID)
	    	folder.setIsAdmin(new java.lang.Integer(1))
	    	folder.setParentFolder(0)
	    	
	    	session_.flush
	    	return usr
	    }catch{ case(e : Exception) => e.printStackTrace}
	    return null
	    
	  return null
	}
}